
package net.atremmedia.gwt.actionjava.examples.sgi;

import actionjava.anim.Anim;
import actionjava.anim.ITween;
import actionjava.anim.config.Manifest;
import actionjava.anim.ease.ExpoIn;
import actionjava.anim.ease.ExpoInOut;
import actionjava.anim.ease.ExpoOut;
import actionjava.anim.events.TweenCompleteHandler;
import actionjava.anim.events.TweenEvent;
import actionjava.anim.events.TweenEvent.TweenEventType;
import actionjava.anim.events.TweenEventParams;
import actionjava.display.Bitmap;
import actionjava.display.BitmapData;
import actionjava.display.Graphics;
import actionjava.display.Shape;
import actionjava.display.Sprite;
import actionjava.display.TextField;
import actionjava.geom.Color;

public class ImgGalleryDim extends Sprite
{
	private Shape itemMask;
	private ITween tween1 = null;
	private ITween tween2 = null;
	private ITween tween3 = null;
	private ITween tween4 = null;
	private TextField textF = null;
	private Bitmap logoBitmap;

	public ImgGalleryDim()
	{
		initialize();
	}

	private void initialize()
	{
		createMask();
		setAlpha(0);
		setX(-270);
		setY(-100);
		setMask(itemMask);
		createLogo();
	}

	private void createMask()
	{
		itemMask = new Shape();
		Graphics graph = itemMask.getGraphics();
		graph.beginStroke("#000");
		graph.beginFill(Color.getRGBA(0xFF0000, 1));
		try {
			graph.decodePath("AmWPnQr7hJnpnMIwR25MBUXAAAIAAfOg");
		} catch (Exception e) {
			e.printStackTrace();
		}
		graph.closePath();
	}

	private void createDim(String color)
	{
		Graphics graph = getGraphics();
		graph.clear();
		graph.beginFill(color);
		graph.drawRect(0, 0, 540, 200);
		graph.endFill();
	}

	private TextField createText(double x, double y, String text)
	{
		textF = new TextField(text, "Arial", "#FFFFFF");
		textF.setSize(15);
		textF.setX(x);
		textF.setY(y);
		return textF;
	}

	private void createLogo()
	{
		logoBitmap = new Bitmap(new BitmapData("IMG/sgiExample/logo2.png"));
		logoBitmap.setX(250);
		logoBitmap.setY(75);
		addChild(logoBitmap);
	}

	public void setColor(String color)
	{
		createDim(color);
	}

	public void setText(double x, double y, String text)
	{
		if(textF != null) {
			removeChild(textF);
		}

		addChild(createText(x, y, text));
	}

	public void animateIn(final double x, double y)
	{
		if(tween2 != null) {
			tween2.pause();
		}

		setAlpha(0);

		tween1 = Anim.tween(new Manifest(this, .75, ExpoOut.ease)
		{
			@Override
			protected void configure()
			{
				to(ALPHA, .85);

				addListener(TweenEventType.COMPLETE, new TweenCompleteHandler()
				{
					@Override
					protected void tweenComplete(TweenEvent event, TweenEventParams params)
					{
						animateOut();
					}
				});
			}
		});

		logoBitmap.setAlpha(0);
		logoBitmap.setX(225);

		tween2 = Anim.tween(new Manifest(logoBitmap, .75, ExpoOut.ease)
		{
			@Override
			protected void configure()
			{
				to(X, 250);
				to(ALPHA, 1);
			}
		});

		textF.setAlpha(0);
		textF.setX(310);

		tween3 = Anim.tween(new Manifest(textF, .75, ExpoOut.ease)
		{
			@Override
			protected void configure()
			{
				to(X, x);
				to(ALPHA, 1);
			}
		});
	}

	public void animateOut()
	{
		if(tween1 != null) {
			tween1.pause();
			tween2.pause();
			tween3.pause();

		}

		setAlpha(.85);

		tween4 = Anim.tween(new Manifest(this, 1, ExpoIn.ease)
		{
			@Override
			protected void configure()
			{
				to(ALPHA, 0);
			}
		});

		tween2 = Anim.tween(new Manifest(logoBitmap, 1, ExpoInOut.ease)
		{
			@Override
			protected void configure()
			{
				to(X, 275);
				to(ALPHA, 0);
			}
		});

		tween3 = Anim.tween(new Manifest(textF, 1, ExpoInOut.ease)
		{
			@Override
			protected void configure()
			{
				to(X, 225);
				to(ALPHA, 0);
			}
		});
	}
}
